--jackport 滚动数字
local ScrollDigital = class("ScrollDigital", cc.Node)
local tbnumpy = {-361,-323,-285,-247,-209,-171,-133,-95,-57,-19}
local defulp = 9
function ScrollDigital:ctor(num,distance)
  
    local clipSp = cc.Sprite:create("public/scroll_digital_bg2.png")
    local clip = cc.ClippingNode:create()
    clip:setInverted(false)
    clip:setStencil(clipSp)
    clip:setAlphaThreshold(0)
    clip:addTo(self)

    local digitalNode = cc.Node:create()
    clip:addChild(digitalNode)

    local dx = 29.5
    if distance ~= nil then
        dx = distance
    end
	self.dx = dx
    self.mDigitalLabels = {}
    for i=1,9 do
	--	defulp = i
	--local tempnum = clone(num)
	--	tempnum = string.sub(tempnum,i,i)
        local label
		if i == 7 then
			label= cc.Sprite:create("public/scroll_digital2.png")
			label:setAnchorPoint(cc.p(0,0))
			label:setPosition(-112+dx*(i-1),-437)
		else
			label= cc.Sprite:create("public/scroll_digital3.png")
			label:setAnchorPoint(cc.p(0,0))
			label:setPosition(-112+dx*(i-1),tbnumpy[defulp+1])
		end
       -- :setString("0")
        label:addTo(digitalNode)
		label.num = defulp
        table.insert(self.mDigitalLabels,label)
    end
    self:setDigital_pool(num)
	  self.mNum = num
end

function ScrollDigital:getNum()
    return self.mNum
end

function ScrollDigital:updateDigital(num,animatTime,ispool)
	num = num or self.mNum--+0.01
	self.lastisBigPrice = not ispool
	animatTime = animatTime or 1
  --  if num ~= self.mNum then
		self:stopAllActions()
        self.mNum = num
        local snum = string.format("%.2f", num)
		local mf = string.split(snum,".")
		snum = string.format("%.6d", mf[1]).."."..mf[2]
        local lidx = 9
        for i=1,9 do
			self.mDigitalLabels[i]:stopAllActions()
			if i ~= 7 then
				self.mDigitalLabels[i]:setPositionY(-19)
				self.mDigitalLabels[i].num = 9
			end
        end
		animatTime = animatTime -1 > 0 and  animatTime -1 or 0.01
	--	snum = "000038.25"
        self:runAnimationTonum(9,lidx,snum,animatTime)
  --  end
end
local scrolly = 70
function ScrollDigital:runAnimationTonum(idx,lableidx,snum,animatTime)
	for i=1,9 do
		if i ~= 7 then
			local labx =  self.mDigitalLabels[i]:getPositionX()
			local t = math.random(200,300)
			local act1 = cc.MoveTo:create(t/1000,cc.p(labx,-400))
			local act2 = cc.CallFunc:create(function ()
				self.mDigitalLabels[i]:setPositionY(-19)
				if self.mDigitalLabels[i].bsetstring then
				--	print("stiop@@@",i)
					self.mDigitalLabels[i].bsetstring = false
					self.mDigitalLabels[i]:stopAllActions()
					local num = self.mDigitalLabels[i].num
				---	local stoptiem = -tbnumpy[num + 1]/350
					local act1_1 = cc.MoveTo:create(0.01,cc.p(labx,tbnumpy[num + 1]))
					local act2_2 = cc.CallFunc:create(function ()
						self.mDigitalLabels[i]:setPositionY(tbnumpy[num + 1]-scrolly)
					end)
					local act3_3 = cc.EaseElasticOut:create(cc.MoveBy:create(0.2,cc.p(0,scrolly)))
					self.mDigitalLabels[i]:runAction(cc.Sequence:create(act1_1,act2_2,act3_3))
				end
			end)
			self.mDigitalLabels[i]:runAction(cc.RepeatForever:create(cc.Sequence:create(act1,act2)))
		end
	end
	performWithDelay(self,function ()
		self:runDigitalAction(idx,lableidx,snum)
	end,animatTime)
end
function ScrollDigital:runDigitalAction(idx,lableidx,snum)
    local digital = string.sub(snum,idx,idx)
    if self.mDigitalLabels[lableidx].num ~= digital and digital ~= "."   then
      --  self.mDigitalLabels[lableidx]:stopAllActions()
    --    local px = self.mDigitalLabels[lableidx]:getPositionX()
       -- self.mDigitalLabels[lableidx]:setPositionY(tbnumpy[digital + 1])
        self.mDigitalLabels[lableidx].bsetstring = true
        self.mDigitalLabels[lableidx].num = digital
     --   self.mDigitalLabels[lableidx]:runAction(cc.EaseElasticOut:create(cc.MoveBy:create(0.1,cc.p(0,-100))))
		--[[if idx  == 8 then
			idx = idx -1 
			lableidx = lableidx - 1
		end--]]
        if idx-1 >= 1 and lableidx-1 >= 1 then
            self:runAction(cc.Sequence:create(cc.DelayTime:create(0.6), cc.CallFunc:create(function ()
            self:runDigitalAction(idx-1,lableidx-1,snum)
            end)))
	    else
			self.isStopPool =false
        end
    else
		 self.mDigitalLabels[lableidx].bsetstring = true
		self.mDigitalLabels[lableidx].num = 0
        if idx-1 >= 1 and lableidx-1 >= 1 then
            self:runDigitalAction(idx-1,lableidx-1,snum)
		else
			self.isStopPool =false
        end
    end
end
--------------------------
function ScrollDigital:updateDigital_pool(num)
	--	print("updateDigital_pool",num)
	if tonumber(num)<30000 or self.isStopPool then
		return
	end
	if self.lastisBigPrice then --判断上一次开的是不是大奖 如果是大奖就重新滚动恢复正常奖金池
		self:updateDigital(num,2,true)
		self.isStopPool = true  --恢复奖池的时候数字在滚动这个字段控制滚动期间不刷新数值
		return
	end
    if num ~= self.mNum then
	--	self:stopAllActions()
        self.mNum = num
        local snum = string.format("%.2f", num)
        local lidx = 9
        for i=1,9-#snum do
			--if i ~= 7 then
				self.mDigitalLabels[i]:setPositionY(tbnumpy[1])
				self.mDigitalLabels[i].num = 0
			--end
        end
        self:runDigitalAction__pool(#snum,lidx,snum)
    end
end
function ScrollDigital:setDigital_pool(num)
	--	print("updateDigital_pool",num)
	if tonumber(num)<30000 or self.isStopPool then
		return
	end
    if num ~= self.mNum then
	--	self:stopAllActions()
        self.mNum = num
        local snum = string.format("%.2f", num)
        local lableidx = 9
        for i=1,9-#snum do
			--if i ~= 7 then
				self.mDigitalLabels[i]:setPositionY(tbnumpy[1])
				self.mDigitalLabels[i].num = 0
			--end
        end
		local idx = #snum
       -- self:runDigitalAction__pool(,lableidx,snum)
		for i = idx,1,-1 do
			 local digital = string.sub(snum,i,i)
			if self.mDigitalLabels[lableidx].num ~= digital and  digital ~= "." then
				local px = self.mDigitalLabels[lableidx]:getPositionX()
				self.mDigitalLabels[lableidx]:setPosition(cc.p(px,tbnumpy[digital + 1]))
				self.mDigitalLabels[lableidx].num = digital
			end
			 lableidx = lableidx - 1
		end
    end
end

function ScrollDigital:runDigitalAction__pool(idx,lableidx,snum)
    local digital = string.sub(snum,idx,idx)
    if self.mDigitalLabels[lableidx].num ~= digital and digital ~= "." then
        self.mDigitalLabels[lableidx]:stopAllActions()
        local px = self.mDigitalLabels[lableidx]:getPositionX()
	--	print("asdfa",idx,lableidx,snum,digital)
        self.mDigitalLabels[lableidx]:setPosition(cc.p(px,tbnumpy[digital + 1]-scrolly))
        self.mDigitalLabels[lableidx].num = digital
        --self.mDigitalLabels[lableidx]:setString(digital)
        self.mDigitalLabels[lableidx]:runAction(cc.EaseElasticOut:create(cc.MoveBy:create(0.2,cc.p(0,scrolly))))
        if idx-1 >= 1 and lableidx-1 >= 1 then
            self:runAction(cc.Sequence:create(cc.DelayTime:create(0.3), cc.CallFunc:create(function ()
            self:runDigitalAction__pool(idx-1,lableidx-1,snum)
            end)))
        end
    else
        if idx-1 >= 1 and lableidx-1 >= 1 then
            self:runDigitalAction__pool(idx-1,lableidx-1,snum)
        end
    end
end

return ScrollDigital